# 构建阶段
FROM python:3.9-slim AS builder

ENV TZ="Asia/Shanghai" TimeZone="Asia/Shanghai" LANG=C.UTF-8 PATH="/usr/local/bin:$PATH"
RUN sed -i 's|http://deb.debian.org|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources  \
    && sed -i 's|http://security.debian.org|https://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list.d/debian.sources \
    && apt-get update && apt-get install -y \
        libssl-dev \
        libbz2-dev \
        libffi-dev \
        libgl1-mesa-glx \
        build-essential \
        cmake \
        python3-dev \
        libopenblas-dev \
        libglib2.0-0 \
        libsm6 \
        libxrender1 \
        libxext6 \
    && rm -rf /var/lib/apt/lists/* 

WORKDIR /app
COPY requirements.txt .
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip setuptools wheel && \
    pip install -i https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir -r requirements.txt python-multipart && \
    rm -rf /tmp/* 

# 运行阶段
FROM python:3.9-slim

ENV TZ="Asia/Shanghai" TimeZone="Asia/Shanghai" LANG=C.UTF-8 PATH="/usr/local/bin:$PATH"
RUN sed -i 's|http://deb.debian.org|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources  \
    && sed -i 's|http://security.debian.org|https://mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list.d/debian.sources \
    && apt-get update && apt-get install -y \
        libgl1-mesa-glx \
        libglib2.0-0 \
        libsm6 \
        libxrender1 \
        libxext6 \
        libopenblas-dev \
    && rm -rf /var/lib/apt/lists/* 

RUN adduser --disabled-password --gecos '' appuser
USER appuser
WORKDIR /app

COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
COPY --chown=appuser:appuser . .

# 暴露端口，根据实际项目修改
EXPOSE 8200

# 启动命令，根据实际项目修改
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8200"]
